﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Corina.Webservice.Cydee
{
    public partial class LoadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string 
            string _folder = WebConfigurationManager.AppSettings["directory_upload"] + Request.QueryString["f"] + "\\";

            using (FileStream fs = new FileStream(_folder + Request.QueryString["n"], FileMode.Open))
            {
                // send data 30 KB at a time
                Byte[] t = new Byte[30 * 1024];

                int bytesRead = 0;
                bytesRead = fs.Read(t, 0, t.Length);
                Response.BufferOutput = false;

                int totalBytesSent = 0;

                Debug.WriteLine("Commence streaming...");
                while (bytesRead > 0)
                {
                    // write bytes to the response stream
                    Response.BinaryWrite(t);

                    // write to output how many bytes have been sent
                    totalBytesSent += bytesRead;
                    Debug.WriteLine("Server sent total " + totalBytesSent + " bytes.");

                    // read next bytes
                    bytesRead = fs.Read(t, 0, t.Length);
                }
            }

            Debug.WriteLine("Done.");

            // ensure all bytes have been sent and stop execution
            Response.End();
        }
    }
}